/* Author: David McCoy dave.mccoy@cospandesign.com
 *
 *     This file is part of DLCM\SubServ.
 *
 *  DLCM\SubServ is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  DLCM\SubServ is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with DLCM\SubServ.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/*
 * PaintWidget.java
 *
 * Send MasterPeices across LCM/DLCM
 *
 * This widget would not have been made without the work done by
 * Timothy Boudreau and the PaintCanvas he wrote for the NetBeans Tutorial
 * Thanks!
 *
 * Created on Apr 3, 2010, 11:12:32 PM
 */

package com.cospandesign.gui.pubsub.widgets;

import pubsub.PubSubManager;
import dlcm.Member;
import dlcm.Structure;
import java.awt.Color;

public class PaintWidget extends javax.swing.JFrame implements Widget{

    String ChannelName;
    String MemberName;
    dlcm.Structure mStructure;
    dlcm.Member mMember;
    dlcm.Structure mImageStructure;
    dlcm.Member WidthMember;
    dlcm.Member HeightMember;
    dlcm.Member SizeMember;
    dlcm.Member ImageDataMember;
    PubSubManager Parent;
    Color SelectedColor;
    //BufferStrategy mBufferStrategy;

    /** Creates new form PaintWidget */
    public PaintWidget() {
        initComponents();
        SelectedColor = Color.BLACK;
        //PaintCanvas.createBufferStrategy(2);
        //mBufferStrategy = PaintCanvas.getBufferStrategy();
        ColorPanel.setBackground(SelectedColor);

        paintComponent.setDiam(10);
        paintComponent.setPaint(SelectedColor);
    }

    // <editor-fold defaultstate="collapsed" desc="Static Functions">
    public static String GetWidgetName() {
        return "Paint Widget";
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Inherited Widget Functions">
    public void ReceiveMessage(Structure structure) {
        Structure paintStructure = (Structure) structure.getMember(MemberName).Data;

        int width = (Integer)paintStructure.getMember("width").Data;
        int height = (Integer)paintStructure.getMember("height").Data;
        int size = (Integer)paintStructure.getMember("size").Data;
        Integer[] xargb = (Integer[])paintStructure.getMember("image_data").getData();
        //int [] argb = (int[])paintStructure.getMember("image_data").Data;
        int [] argb = new int [xargb.length];
        for (int i = 0; i < xargb.length; i ++){
            argb[i] = xargb[i];
        }
        paintComponent.setRawImage(width, height, argb);
    }

    public boolean CanUseMember(Member member) {
       boolean width = false;
       boolean height = false;
       boolean size = false;
       boolean image_data = false;
        if (member.Data instanceof dlcm.Structure){
            dlcm.Structure videoStructure = (Structure) member.Data;

            for (Member m : videoStructure.getMemberList()){
                if (m.Name.matches("width")){
                    width = true;
                }
                else if (m.Name.matches("height")){
                    height = true;
                }
                else if (m.Name.matches("size")){
                    size = true;
                }
                else if (m.Name.matches("image_data")){
                    image_data = true;
                }
            }
        }
        return (width && height && size && image_data);
    }

    public void SetupWidget(String channelName, String memberName, Structure structure, PubSubManager parent) {
        ChannelName = channelName;
        MemberName = memberName;
        mStructure = structure;
        mMember = mStructure.getMember(memberName);
        Parent = parent;
        mImageStructure = (Structure) mMember.Data;
        this.WidthMember = mImageStructure.getMember("width");
        this.HeightMember = mImageStructure.getMember("height");
        this.SizeMember = mImageStructure.getMember("size");
        this.ImageDataMember = mImageStructure.getMember("image_data");

        setTitle(memberName + " Paint Widget");
        PublishButton.setEnabled(true);
        setVisible(true);
    }

    public void setWidgetVisibility(boolean visible) {
        setVisible(visible);
    }
    // </editor-fold>

    public void PublishImage (){
        WidthMember.Data = paintComponent.getImageWidth();
        HeightMember.Data = paintComponent.getImageHeight();
        SizeMember.Data = paintComponent.getImageSize();
        ImageDataMember.Data = paintComponent.getRawImage();
        Parent.PublishStructure(ChannelName, mStructure);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        ColorChooserDialog = new javax.swing.JDialog();
        ColorChooserWindow = new javax.swing.JColorChooser();
        SelectColorButton = new javax.swing.JButton();
        ColorButton = new javax.swing.JButton();
        ColorPanel = new javax.swing.JPanel();
        ClearButton = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        paintComponent = new com.cospandesign.gui.pubsub.widgets.GraphicsPanel.PaintComponent();
        PublishButton = new javax.swing.JButton();

        ColorChooserDialog.setTitle("Color Chooser");
        ColorChooserDialog.setMinimumSize(new java.awt.Dimension(480, 450));
        ColorChooserDialog.setName("ColorChooserDialog"); // NOI18N
        ColorChooserDialog.setResizable(false);

        ColorChooserWindow.setName("ColorChooserWindow"); // NOI18N
        ColorChooserWindow.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                ColorChooserWindowMousePressed(evt);
            }
        });
        ColorChooserWindow.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                ColorChooserWindowPropertyChange(evt);
            }
        });

        SelectColorButton.setText("Select");
        SelectColorButton.setName("SelectColorButton"); // NOI18N
        SelectColorButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SelectColorButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout ColorChooserDialogLayout = new javax.swing.GroupLayout(ColorChooserDialog.getContentPane());
        ColorChooserDialog.getContentPane().setLayout(ColorChooserDialogLayout);
        ColorChooserDialogLayout.setHorizontalGroup(
            ColorChooserDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ColorChooserDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(ColorChooserWindow, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, ColorChooserDialogLayout.createSequentialGroup()
                .addContainerGap(378, Short.MAX_VALUE)
                .addComponent(SelectColorButton)
                .addContainerGap())
        );
        ColorChooserDialogLayout.setVerticalGroup(
            ColorChooserDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ColorChooserDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(ColorChooserWindow, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                .addComponent(SelectColorButton)
                .addContainerGap())
        );

        setIconImage(java.awt.Toolkit.getDefaultToolkit().getImage(getClass().getResource("/com/cospandesign/resources/cospandesign.png")));

        ColorButton.setText("Color Chooser");
        ColorButton.setName("ColorButton"); // NOI18N
        ColorButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ColorButtonActionPerformed(evt);
            }
        });

        ColorPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        ColorPanel.setName("ColorPanel"); // NOI18N

        javax.swing.GroupLayout ColorPanelLayout = new javax.swing.GroupLayout(ColorPanel);
        ColorPanel.setLayout(ColorPanelLayout);
        ColorPanelLayout.setHorizontalGroup(
            ColorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 29, Short.MAX_VALUE)
        );
        ColorPanelLayout.setVerticalGroup(
            ColorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 10, Short.MAX_VALUE)
        );

        ClearButton.setText("Clear");
        ClearButton.setName("ClearButton"); // NOI18N
        ClearButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ClearButtonActionPerformed(evt);
            }
        });

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "5", "10", "20" }));
        jComboBox1.setSelectedIndex(2);
        jComboBox1.setName("jComboBox1"); // NOI18N

        jLabel1.setText("Paint Size");
        jLabel1.setName("jLabel1"); // NOI18N

        paintComponent.setName("paintComponent"); // NOI18N

        javax.swing.GroupLayout paintComponentLayout = new javax.swing.GroupLayout(paintComponent);
        paintComponent.setLayout(paintComponentLayout);
        paintComponentLayout.setHorizontalGroup(
            paintComponentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 632, Short.MAX_VALUE)
        );
        paintComponentLayout.setVerticalGroup(
            paintComponentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 496, Short.MAX_VALUE)
        );

        PublishButton.setText("Publish");
        PublishButton.setEnabled(false);
        PublishButton.setName("PublishButton"); // NOI18N
        PublishButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PublishButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(paintComponent, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(ColorButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(ColorPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(ClearButton, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 342, Short.MAX_VALUE)
                        .addComponent(PublishButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(PublishButton)
                        .addComponent(ClearButton))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(ColorButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(ColorPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(paintComponent, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ColorButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ColorButtonActionPerformed
        if (this.ColorChooserDialog.isVisible()){
            this.ColorChooserDialog.setVisible(false);
        } else {
            this.ColorChooserDialog.setVisible(true);
        }
}//GEN-LAST:event_ColorButtonActionPerformed

    private void ClearButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ClearButtonActionPerformed
        this.paintComponent.clear();
}//GEN-LAST:event_ClearButtonActionPerformed

    private void ColorChooserWindowMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ColorChooserWindowMousePressed

}//GEN-LAST:event_ColorChooserWindowMousePressed

    private void ColorChooserWindowPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_ColorChooserWindowPropertyChange

}//GEN-LAST:event_ColorChooserWindowPropertyChange

    private void SelectColorButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SelectColorButtonActionPerformed
        SelectedColor = this.ColorChooserWindow.getColor();
        ColorPanel.setBackground(SelectedColor);
        paintComponent.setPaint(SelectedColor);
}//GEN-LAST:event_SelectColorButtonActionPerformed

    private void PublishButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PublishButtonActionPerformed
        PublishImage();
    }//GEN-LAST:event_PublishButtonActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PaintWidget().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ClearButton;
    private javax.swing.JButton ColorButton;
    private javax.swing.JDialog ColorChooserDialog;
    private javax.swing.JColorChooser ColorChooserWindow;
    private javax.swing.JPanel ColorPanel;
    private javax.swing.JButton PublishButton;
    private javax.swing.JButton SelectColorButton;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private com.cospandesign.gui.pubsub.widgets.GraphicsPanel.PaintComponent paintComponent;
    // End of variables declaration//GEN-END:variables

}
